import { reqGetCategory1List, reqGetCategory2List, reqGetCategory3List } from '@/api/product/category'

const state = {
  //分类列表
  category1List: [],
  category2List: [],
  category3List: [],
  //分类id
  category1Id: '',
  category2Id: '',
  category3Id: ''
}

const actions = {
  async getCateGory1List({ commit }) {
    const result = await reqGetCategory1List()
    if (result.code === 200) {
      commit('SAVE_CATEGORY1_LIST', result.data)
    }
  },
  async getCateGory2List({ commit }, category1Id) {
    const result = await reqGetCategory2List(category1Id)
    if (result.code === 200) {
      commit('SAVE_CATEGORY2_LIST', { category2List: result.data, category1Id })
    }
  },
  async getCateGory3List({ commit }, category2Id) {
    const result = await reqGetCategory3List(category2Id)
    if (result.code === 200) {
      commit('SAVE_CATEGORY3_LIST', { category3List: result.data, category2Id })
    }
  }
}

const mutations = {
  SAVE_CATEGORY1_LIST(state, category1List) {
    state.category1List = category1List
  },
  SAVE_CATEGORY2_LIST(state, { category2List, category1Id }) {
    state.category2List = category2List
    state.category1Id = category1Id

    state.category2Id = ''
    state.category3Id = ''
    state.category3List = []
  },
  SAVE_CATEGORY3_LIST(state, { category3List, category2Id }) {
    state.category3List = category3List
    state.category2Id = category2Id

    state.category3Id = ''
  },
  SAVE_CATEGORY3_ID(state, category3Id) {
    state.category3Id = category3Id
  },
  RESET(state) {
    state.category1Id = ''
    state.category2Id = ''
    state.category3Id = ''
    state.category2List = []
    state.category3List = []
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
